﻿using Open.SchoolBase.ExpressionVisitor;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Wetland.DBAttribute;
using Wetland.Models;
using Wetland.SqlServer;

namespace Wetland.Test
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                //XmlConfigModel xmlConfig = ReadXML();
                //if (xmlConfig == null || xmlConfig.State==false)
                //{
                //    return;
                //}
                //MassiveContext<YY_DATA_AUTO> context = new MassiveContext<YY_DATA_AUTO>
                //                                      (xmlConfig.DBWriteConnection,
                //                                       xmlConfig.DBReadConnection,
                //                                       xmlConfig.DBName,
                //                                       xmlConfig.TableName
                //                                       );

                //MassiveContext<YY_DATA_AUTO> context = new MassiveContext<YY_DATA_AUTO>();


                string r = "data source=218.249.12.119,1599;uid=netdb;pwd=qwer135@$Dbs;";
                string w = "data source=218.249.12.119,1599;uid=netdb;pwd=qwer135@$Dbs;";
                string SYS_Wetland_DB = "SYS_Wetland_DB";
                string SYS_Wetland_Rel = "SYS_Wetland_Rel";

                MassiveContext<YY_DATA_AUTO> context = new MassiveContext<YY_DATA_AUTO>(r, w, SYS_Wetland_DB, SYS_Wetland_Rel);
                //List<YY_DATA_AUTO> entitys = new List<YY_DATA_AUTO>();
                //测试写入，按天水平分库
                //for (int i = 0; i < 10; i++)
                //{
                //    YY_DATA_AUTO entity = new YY_DATA_AUTO();
                //    entity.STCD = "172017155158";
                //    entity.ItemID = "0003029";
                //    entity.DATAVALUE = 10;
                //    entity.DOWNDATE = DateTime.Now;
                //    entity.TM = DateTime.Now;
                //    entity.STTYPE = "";
                //    entity.NFOINDEX = 1;
                //    entity.CorrectionVALUE = 11;
                //    context.Add(entity);
                //    Console.WriteLine(entity.STCD + "写入成功!!...");
                //    Thread.Sleep(1000);
                //}
                //批量写入
                //context.AddList(entitys);

                Stopwatch sw = new Stopwatch();
                sw.Start();
                #region 条件
                var startTime = DateTime.Parse("2020-05-11 00:00:00");
                var endTime = DateTime.Parse("2020-05-11 23:59:59");
                //Expression<Func<YY_DATA_AUTO, bool>> where = p => p.STCD == "C79FE7335DC6BADB" 
                //&&p.ItemID == "1002" 
                //&& p.TM >= startTime && p.TM <= endTime;
                //p => p.TM >= startTime && p.TM <= endTime;
                #endregion 
                Expression<Func<YY_DATA_AUTO, bool>> where =p=> true; 
                DBPageBase page = new DBPageBase();
                page.PageIndex = 1500;
                page.PageSize = 2000;
                var result = context.Get(where, page);
                sw.Stop();
                Console.WriteLine("用时:" + sw.ElapsedMilliseconds);


                var updateEntity = new YY_DATA_AUTO();
                updateEntity.STCD = "08217687511767A5";
                updateEntity.ItemID = "1002";
                updateEntity.TM = DateTime.Parse("2020-05-07 09:18:00");
                updateEntity.DATAVALUE = (decimal)24.7;
                updateEntity.DOWNDATE = DateTime.Parse("2020-04-25 19:11:32");
                updateEntity.STTYPE = "";
                updateEntity.NFOINDEX = 1;
                updateEntity.CorrectionVALUE = (decimal)24.7;
                updateEntity.DATATYPE = 3;

                Expression<Func<YY_DATA_AUTO, bool>> updateWhereSql = p => 
                                                   p.STCD == updateEntity.STCD  && p.ItemID == updateEntity.ItemID && p.TM==updateEntity.TM;
                //context.Update(updateEntity, updaetWhereSql, null, updateEntity.TM);

                //var exist=context.Get(updateWhereSql, updateEntity.TM);
                //YY_DATA_AUTO info= context.Get(updaetWhereSql, updateEntity.TM);
                //context.Delete(updaetWhereSql, updateEntity.TM);
                //context.Update(updateEntity, 47429073,null,updateEntity.TM);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.ReadLine();
            }

        }

        public static XmlConfigModel ReadXML()
        {
            XmlConfigModel xcm = new XmlConfigModel();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "/xml/Wetland.xml");
            XmlNode node = xmlDoc.SelectSingleNode("Wetland").SelectSingleNode("State");
            xcm.State = node.InnerText == "1" ? true : false;
            xcm.DBWriteConnection = xmlDoc.SelectSingleNode("Wetland").SelectSingleNode("DBWriteConnection").InnerText;
            xcm.DBReadConnection = xmlDoc.SelectSingleNode("Wetland").SelectSingleNode("DBReadConnection").InnerText;
            xcm.DBName = xmlDoc.SelectSingleNode("Wetland").SelectSingleNode("DBName").InnerText;
            xcm.TableName = xmlDoc.SelectSingleNode("Wetland").SelectSingleNode("TableName").InnerText;
            return xcm;
        }



    }



}
